import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/map',
    name: 'map',
    component: () => import('@/views/Map.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    redirect: '/map'
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('@/views/Admin.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/admin/user',
    name: 'manageuser',
    component: () => import('@/views/ManageUser.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/admin/properties',
    name: 'manageproperties',
    component: () => import('@/views/ManageProperties.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/admin/footprints',
    name: 'managefootprints',
    component: () => import('@/views/ManageFootprint.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
  },
  {
    path: '/my-footprints', // 新增“我的足迹”页面
    name: 'my-footprints',
    component: () => import('@/views/Footprints.vue'),
    meta: { requiresAuth: true }
  },
    {
   path: '/recommendations',
   name: 'Recommendations',
   component: () => import('@/views/Recommendation.vue')
  },
    {
    path: '/recommendations/:id',
    name: 'RecommendationDetail',
    component: () => import('@/views/RecommendationDetail.vue')
  },
  {
    path: '/profile',
    name: 'profile',
    component: () => import('@/views/Profile.vue'),
    meta: { requiresAuth: true } // 任何登录用户都可以访问
  },
  {
    path: '/profile/myfavorite',
    name: 'favorite',
    component: () => import('@/views/Footprints.vue'),
    meta: { requiresAuth: true } // 任何登录用户都可以访问
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore();

  // 确保认证状态已检查完毕
  if (!authStore.statusChecked) {
    await authStore.checkAuthStatus();
  }

  const isAuthenticated = authStore.isAuthenticated;
  const isAdmin = authStore.isAdmin;

  // 1. 目标路由需要管理员权限
  if (to.meta.requiresAdmin) {
    if (isAuthenticated && isAdmin) {
      next(); // 管理员，已认证 -> 允许
    } else if (isAuthenticated && !isAdmin) {
      next('/map'); // 已认证但非管理员 -> 重定向到地图页 (或无权限页)
    } else {
      next({ name: 'login', query: { redirect: to.fullPath } }); // 未认证 -> 重定向到登录
    }
  }
  // 2. 目标路由需要普通认证 (非管理员特定)
  else if (to.meta.requiresAuth) {
    if (isAuthenticated) {
      next(); // 已认证 -> 允许
    } else {
      next({ name: 'login', query: { redirect: to.fullPath } }); // 未认证 -> 重定向到登录
    }
  }
  // 3. 目标路由是登录页
  else if (to.name === 'login' && isAuthenticated) {
     // 如果用户已登录，尝试访问登录页则重定向到其角色对应的首页
     next(isAdmin ? '/admin' : '/map');
  }
  // 4. 目标路由不需要任何认证 (例如公共地图页 /map, 登录页 /login 对于未登录用户)
  else {
    next(); // 允许访问
  }
});

export default router